package com.bluesky.jwf.component.tab;

import com.bluesky.javawebbrowser.domain.html.tags.A;
import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.HyperLinkButton;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;
import com.bluesky.jwf.component.list.ListItem;

;

public class TitleListItem extends ListItem {
	private HyperLinkButton name;

	private Tag itemTag;
	private HyperLinkButton closeBtn;

	protected EventHandler queryCloseHandler;

	protected EventHandler closeHandler;

	@Override
	public void init(String html) {

		super.init(html);

		itemTag = loadByJwfClass("item");

		A nameTag = (A) itemTag.loadByJwfClass("name");
		name = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent(HyperLinkButton.class.getSimpleName());
		nameTag.replace(name);
		name.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {

				Tabs tabs = (Tabs) getAncesterByClass(Tabs.class);

				tabs.setActiveTab(getIndex());
			}
		});

		A closeBtnTag = (A) itemTag.loadByJwfClass("closeButton");
		closeBtn = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent(HyperLinkButton.class.getSimpleName());
		closeBtnTag.replace(closeBtn);
		closeBtn.setText("[X]");
		closeBtn.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				onClose();
			}
		});

	}

	/**
	 * event Close.
	 */
	private void onClose() {
		EventHandler onCompleteQueryHandler = new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				Boolean canClose = (Boolean) parameter;
				if (canClose)
					close();

			}
		};

		if (queryCloseHandler != null)
			queryCloseHandler.handle(this, null, onCompleteQueryHandler);
		else
			close();
		
		/*if multi-thread is supported, the code would be much simpler. 
		 *posedu code:<br>
		 *if(queryCloseHandler!=null)<br>
		 *	queryCloseHandler.handler();<br>
		 *<br>
		 *close(); 
		 *
		 */
		 
	}

	/**
	 * a close method, do close.
	 */
	private void close() {

		EventHandler doDestroy = new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				destroy();
			}
		};

		if (closeHandler != null)
			closeHandler.handle(this, null, doDestroy);
		else
			doDestroy.handle(this, null, null);
	}

	private void destroy() {
		Tab tab = (Tab) model;
		tab.getTabs().removeTab(tab);
	}

	public EventHandler getQueryCloseHandler() {
		return queryCloseHandler;
	}

	public void setQueryCloseHandler(EventHandler queryCloseHandler) {
		this.queryCloseHandler = queryCloseHandler;
	}

	@Override
	public void setModel(Object model) {

		super.setModel(model);

		Tab tab = (Tab) model;
		name.setText(tab.getName());

		queryCloseHandler = tab.getQueryCloseHandler();
		closeHandler = tab.getCloseHandler();

		closeBtn.setVisible(tab.isActive());
		if (tab.isActive())
			itemTag.setAttribute("class", "tabs-title-item-active");
		else
			itemTag.setAttribute("class", "tabs-title-item");
	}
}
